Help needed: automation that generates specific timeformat and find corresponding switch

Hi,

I’m looking for an automation that

  • runs regulary
  • translate the current time in a certain timeformat “xxyy” where xx= number of current day and yy= current hour
  • checks the state of a switch with the name with this timeformat
  • turns ON/OFF another switch, according to the state found in the above switch

in example, I have 7 x 24 (dummy) switches representing a week schedule, with following name convention

  - platform: mqtt
    name:          "scheduler thermostat xxyy"
    state_topic:   "hass/scheduler/thermostat/xxyy"
    command_topic: "hass/scheduler/thermostat/xxyy"
    payload_on:    "1"
    payload_off:   "0"
    retain: true
    icon: mdi:timer

Where

  • xx: dateformat (01=monday, 02=thuesday, …, 07=sunday)
  • yy: timeformat (00= 00:00, 01= 01:00, …, 23= 23:00)

Now I want to check every 15 minutes the state of the “current” schedule switch.
I.e. if the automation runs on tuesday 21:07, it should check the state of “switch.scheduler_thermostat_0221” and if the state of this particular switch is “ON”, than turn another switch “switch.scheduler_thermostat_main” also to “ON”

For me the difficulty is translating now() to the timeformat xxyy and to find the corresponding switch.

Any help is much appreciated!

kind regards,
Bart

Okay

I already managed to create this automation:

- alias: scheduler thermostat
  initial_state: 'on'
  trigger:
    platform: time_pattern
    seconds: '/3'
  action:
    - service: mqtt.publish
      data_template:
        topic: 'hass/scheduler/thermostat/main'
        payload_template: "I need a formula for dynamic lookup for switch here"

    - service: input_text.set_value
      data_template:
        entity_id: input_text.text_1
        value: "switch.scheduler_thermostat_0{{ now().weekday() + 1}}{{ now().hour}}"

    - service: input_text.set_value
      data_template:
        entity_id: input_text.text_2
        value: "{{now()}}"

    - service: input_text.set_value
      data_template:
        entity_id: input_text.text_3
        value: "{{ states('switch.scheduler_thermostat_0223')}}"

    - service: input_text.set_value
      data_template:
        entity_id: input_text.text_4
        value_template: "{{ states('switch.scheduler_thermostat_0{{ now().weekday() + 1}}{{ now().hour}}')}}"

text_1: a formula that creates the correct reference to a switch, based on current day and hour, i.e.
switch.scheduler_thermostat_0223

text_2: just a time stamp so I can see my automation is running :slight_smile:

text_3: the state of my switch, with a hardcoded reference to my switch. In this example switch.scheduler_thermostat_0223
Using this hardcoded reference gives the correct output (state of switch is shown)

text_4: the same as text_3, but now with a dynamic reference to the switch, with a formula that generates the correct switch name (formula is used in text_1 and output seems correct)
Using this dynamic reference gives NO output (state of switch is unknown)

04-02-2020 23-43-53

What’s wrong here?

regards,
Bart

Pew. Took me 3hours of time, but hey… time flies when you’re having fun! :frowning:

For those who are interested:

- alias: scheduler thermostat
  initial_state: 'on'
  trigger:
    platform: time_pattern
    minutes: '/15'
  action:
    - service: mqtt.publish
      data_template:
        topic: 'hass/scheduler/thermostat/main'
        payload: "{{ states('switch.scheduler_thermostat_0' ~ (now().strftime('%w%H')))}}"

publishes a mqtt message every 15 minutes with the state of the “current” switch.
Current switch is
switch.scheduler_thermostat_xxyy
where

  • xx=day of the week (01=monday, etc…)
  • yy= hour of the day (00 … 23)

Am I becoming a jinja expert already ? :grinning:

grtz
B