#How to add persistent variables to scripts/helpers?
1 messages · Page 1 of 1 (latest)
Typical approach would be to store it in a helper
how can i say like after x time of light y's state being off change variable z
i need to do something like this c++ if (light.mode(variable i need to make) == "night" && !light.state("my light") && (time since last on >= 8s)) light.mode == "day";
make 2 triggers
light mode state -> "night"
light state off for 8s
Conditions
check both are true
Action
light mode -> "day"
@shy crescent that is in helpers or where? and that runs 24/7?
because if i manually turn light off and its in night mode after 8 seconds it goes back to daytime but ha will never know unless it checks
i need to periodically check (every 30 seconds or so) if light has been off for > 8 seconds and if mode == night then change mode to day
else do nothing
No you don't. You only need to check when the light has been off for 8s if it's night, and (maybe) when it turns night if it's been off for 8s
oh i see what ur saying
In state triggers in automations you can set the trigger to be "light has been off for 8s"
so make a trigger on light turned off (which will trigger when manually done or by a script/ha and then if not turned back on before 8s set mode to day
so in helpers i just need to set a variable what is that called?
there is no "variable" options
idk what word they use for it
Use a Dropdown helper, give it options of day and night
Otherwise a text helper can handle it more generically
then first option?
so confusing lol
i think i have to make an automation lol
doesnt seem to be any action to set the variable to "day"
found it i think
how do i say "if this sequence of events happens in less than X seconds" do y
I think it would be helpful if you describe what you're trying to accomplish.
@brittle apex got it figured out just having an issue now trying to make a script to detect states and modes
color: |
[[[
var lightState = states['light.sw2_b_new_2_light_1'];
var mode = states['input_select.diningroom_light_mode'];
if (lightState === 'on') {
if (mode === 'Day') return 'rgb(255,200,0)';
if (mode === 'Night') return 'rgb(255,100,50)';
return 'rgb(255,255,255)';
} else {
return 'rgb(128,128,128)';
}
]]]```
not sure if this is correct for getting data but doesnt seem to be working
basically i have some lights that when turned on are bright and is the main light but they also have a dim orange secondary outter light for like a night light that is activated by toggling the switch off/on in less than 5 seconds to change the mode
however there is no feedback for what mode it is as the smartr aspect is just a smart dimmer switch
if the light is off for 8+ seconds then it resets the light back to day mode
so i needed to create a way to know what mode it is currently in so i could make a script to switch them to night mode regardless of their current mode (which i finally did lol)
now im trying to make a custom card so i can control them all and their icon color changes based on mode and i have it all working except color changing the icon
type: vertical-stack
cards:
- type: custom:button-card
entity: light.lights
icon: mdi:light-recessed
color_type: icon
name: Lights
show_state: false
show_icon: true
tap_action:
action: perform-action
perform_action: script.toggle_light_all
target: {}
double_tap_action:
action: perform-action
perform_action: script.night_mode_all
target: {}
hold_action:
action: more-info
- type: horizontal-stack
cards:
- type: custom:button-card
entity: light.sw2_b_new_light_1
color_type: icon
color: |
[[[
var mode = states['input_select.livingroom_light_mode'];
if (['light.sw2_b_new_light_1'] === 'on') {
if (mode === 'Day') return 'rgb(255,200,0)';
if (mode === 'Night') return 'rgb(255,100,50)';
return 'rgb(255,255,255)';
} else {
return 'rgb(128,128,128)';
}
]]]
name: Living
icon: mdi:light-recessed
double_tap_action:
action: perform-action
perform_action: script.night_mode
target: {}
hold_action:
action: more-info
- type: custom:button-card
entity: light.sw2_b_new_2_light_1
color_type: icon
color: |
[[[
var lightState = states['light.sw2_b_new_2_light_1'];
var mode = states['input_select.diningroom_light_mode'];
if (lightState === 'on') {
if (mode === 'Day') return 'rgb(255,200,0)';
if (mode === 'Night') return 'rgb(255,100,50)';
return 'rgb(255,255,255)';
} else {
return 'rgb(128,128,128)';
}
]]]
i omitted some of the lights for brevity
top one controls all lights and bottom is single control all is working exactly as i want it except the icon color changing
Try this. Took a little bit of a different route and used the state: styling. Also, used some variables which could cut down on having to change stuff that is hard-coded and/or easy to turn into a configuration template. ```yaml
type: custom:button-card
variables:
mode_helper: input_select.livingroom_light_mode
entity: light.sw2_b_new_light_1
name: Living
icon: mdi:light-recessed
state:
- value: 'on'
styles:
icon:
- color: |
[[[
var mode = states[variables.mode_helper].state;
if (mode == 'Day') return 'rgb(255,200,0)';
if (mode == 'Night') return 'rgb(255,100,50)';
]]] - value: 'off'
styles:
icon:
- color: 'rgb(128,128,128)'
double_tap_action:
action: perform-action
perform_action: script.night_mode
target: {}
hold_action:
action: more-info
trying to make a script to detect states and modes
what you're working on is actually known as a template. not quite the same as a script. the custom:button-card uses JavaScript which you got mostly correct but you also incorporated some elements of Jinja which won't work.
yep that works
@brittle apex why wont something like this work so its easy to add my others?
- type: horizontal-stack
cards:
[[[
// Define your lights list
var lights = [
{ entity: 'light.sw2_b_new_light_1', mode_helper: 'input_select.livingroom_light_mode', night_script: 'script.night_mode', name: 'Living' },
{ entity: 'light.sw2_b_new_2_light_1', mode_helper: 'input_select.diningroom_light_mode', night_script: 'script.night_mode_dining_room', name: 'Dining' },
{ entity: 'light.sw2_b_new_3_light_1', mode_helper: 'input_select.light_mode', night_script: 'script.night_mode_kitchen', name: 'Kitchen' }
];
return lights.map(light => ({
type: 'custom:button-card',
entity: light.entity,
name: light.name,
icon: 'mdi:light-recessed',
color_type: 'icon',
color: `
[[[
var state = states["${light.entity}"].state;
var mode = states["${light.mode_helper}"].state;
if (state === 'on') {
if (mode === 'Day') return 'rgb(255,200,0)';
if (mode === 'Night') return 'rgb(255,100,50)';
return 'rgb(255,255,255)';
} else return 'rgb(128,128,128)';
]]]`,
double_tap_action: {
action: 'perform-action',
perform_action: light.night_script
},
hold_action: { action: 'more-info' }
}));
]]]```
duplicating the same thing but changing names for 5+ lights is lengthy
Because you either got that from AI or you tried to incorporate something from auto entities.
yep its from ai i asked it to do in yaml what i coded in c++ because idk yaml lol
is it not possible?
i would like it in a list so when i add a light i can just add it to the list in 1 line and done
this is the current (working with your method) config/script https://pastebin.com/NwFvWbk0
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
this is 3 lights and its long and a pain to add more haha but does work
a simple loop thru the list method seems doable i would think
That's where auto-entities could possibly be used. The problem you'll run into is that each card has multiple entities it needs to reference (the entity, the helper, and the script). Auto-entities could handle the entity itself. If you wanted to make something like that work, you would need to standardize your naming convention for all three entities and then have the JavaScript "change" the names.
light.livingroom_light, input_select.livingroom_light_mode, script.livingroom_light_night_mode
Configuration templates are also useful. It cuts down on having to repeat code. Variables could be easily implemented, but you'd still have to define each card. Working in YAML is generally quicker than the UI in this regard since you can simple copy & paste what's needed and then just change the entities, names, etc.
ya ud think u could just make an array and loop it 1 time and be done
YAML is not a programming language and cannot perform loops. YAML is just another form of JSON. It essentially just stores data.