#How to add persistent variables to scripts/helpers?

1 messages · Page 1 of 1 (latest)

bleak sinew
#

Would like to add light mode variable to my lights but unsure how

shy crescent
#

Typical approach would be to store it in a helper

bleak sinew
#

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";

shy crescent
#

make 2 triggers
light mode state -> "night"
light state off for 8s

Conditions
check both are true

Action
light mode -> "day"

bleak sinew
#

@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

shy crescent
bleak sinew
#

oh i see what ur saying

shy crescent
#

In state triggers in automations you can set the trigger to be "light has been off for 8s"

bleak sinew
#

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

shy crescent
#

Use a Dropdown helper, give it options of day and night

#

Otherwise a text helper can handle it more generically

bleak sinew
#

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

bleak sinew
#

how do i say "if this sequence of events happens in less than X seconds" do y

brittle apex
#

I think it would be helpful if you describe what you're trying to accomplish.

bleak sinew
#

@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

brittle apex
#

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.

bleak sinew
#

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

brittle apex
#

Because you either got that from AI or you tried to incorporate something from auto entities.

bleak sinew
#

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 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

brittle apex
#

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.

bleak sinew
#

ya ud think u could just make an array and loop it 1 time and be done

brittle apex
bleak sinew
#

have brightness ring lol

#

living is no power right now cause installing more but looks nice

#

it is all working quite nicely