Need correct data_template to set schedule in Hue api using rest command

HI,

what would be the API command to set the day schedule of a motion sensor?

this is the sensor:

{"name":"MotionSensor 48.day-on","owner":"redacted","created":"2019-05-01T07:44:00","lasttriggered":"none","timestriggered":0,"status":"enabled","recycle":true,"conditions":[{"address":"/config/localtime","operator":"in","value":"T07:00:00/T22:30:00"},{"address":"/sensors/48/state/presence","operator":"eq","value":"true"},{"address":"/sensors/48/state/presence","operator":"dx"},{"address":"/sensors/50/state/status","operator":"lt","value":"1"},{"address":"/sensors/49/state/dark","operator":"eq","value":"true"}],"actions":[{"address":"/groups/2/action","method":"PUT","body":{"scene":"xahsEsVMGanjhFk"}},{"address":"/sensors/50/state","method":"PUT","body":{"status":1}}]}

so I know I have to set

http://192.168.1.212/api/redacted/rules/15/

the Put command would be in this form:

http://192.168.1.212/api/redacted/{{ type }}/{{ id }}/{{ command }}

but I am struggling with the correct command.

Getting it is easy: {{ value_json.conditions[0].value }} which is then returned as T07:00:00/T22:30:00 but I how can I set that day-time ?

for reference:

for this sensor at http://192.168.1.212/redacted/sensors/48
with response:

{"state":{"presence":null,"lastupdated":"none"},"swupdate":{"state":"noupdates","lastinstall":"2019-03-12T09:33:35"},"config":{"on":false,"battery":100,"reachable":true,"alert":"none","ledindication":false,"usertest":false,"sensitivity":2,"sensitivitymax":2,"pending":[]},"name":"Master bedroom sensor","type":"ZLLPresence","modelid":"SML001","manufacturername":"Philips","productname":"Hue motion sensor","swversion":"6.1.1.27575","uniqueid":"redacted","capabilities":{"certified":true}}

I can set eg sensitivity as follows:

script:
  set_hue_sensitivity:
    alias: Set Hue sensitivity
    sequence:
      service: rest_command.set_hue_command
      data_template:
        command: config
        type: sensors
        data: >
          {% set mapper =
            { 'Low':'0',
              'Medium':'1',
              'High':'2' } %}
          {% set state = states('input_select.select_hue_sensitivity') %}
          {% set sensitivity = mapper[state] if state in mapper %}
          {"sensitivity": {{sensitivity}} }
        id: >
          {% set mapper =
            { 'Laundry':'22',
              'Dining table':'52',
              'Auditorium':'44',
              'Frontdoor':'60',
              'Dorm':'56',
              'Corridor':'5',
              'Corridor terrace':'33',
              'Master bedroom':'48',
              'Corridor Office':'28',
              'Control room':'7',
              'Attic':'12' } %}
          {% set state = states('input_select.select_hue_motion_sensor') %}
          {% set id = mapper[state] if state in mapper %}
          {{id}}

using that same rest command, I would need to adjust the above data_template to point to the value":“T07:00:00/T22:30:00”

guess I would need a way to replace the current schedule’s daypart (split at /) with the desired setting:

{% set schedule = ‘T06:04:00/T22:30:00’ %}
{% set alarm = states(‘sensor.alarmclock_wd_time’) %}
{{‘T’+alarm+ schedule[6:19]}}

or using an available schedule sensor:

{{states('sensor.master_bedroom_schedule_rst')}}
{% set schedule = states('sensor.master_bedroom_schedule_rst') %}
{% set alarm = states('sensor.alarmclock_wd_time') %}
{{schedule[0:10]+'T'+alarm+':00'}} or {{schedule.split('/')[0] + '/T'+alarm+':00'}}

thanks for having a look!

for reference to this fine community answering myself here…

Hue api has 4 rules for schedules, which need to be set all 4 to change the day and night settings correctly…Added to that, it needs the full set of conditions so all settings under that part in the value need to be added.

I created a template, to have it read my alarm_clock time, {{alarm}} and another template {{schedule.split('/')[0]}}, so it reads the current settings for the night schedule too, and that doesn’t get hard coded in the script, if ever I feel the need to change these too from within HA.

the script is using this rest_command:

rest_comand:
  set_hue_rule:
    url: !secret url_set_hue_rule
    method: put
    payload: '{{ data }}'

and has the form of:

url_set_hue_rule: http://192.168.1.212/api/redacted/{{ type }}/{{ id }}

this is the script I made to do so:

  set_hue_alarm_time_wakeup:
    alias: Set Hue alarm time wakeup
    sequence:
# delay needed to settle the state change of the alarm time properly
      - delay:
          seconds: 2
# "name":"MotionSensor 48.day-on"
      - service: rest_command.set_hue_rule
        data_template:
          type: rules
          id: 15
          data: >
            {% set schedule = states('sensor.master_bedroom_schedule_rst') %}
            {% set alarm = states('sensor.alarmclock_wd_time') %}
            {"conditions":[{"address":"/config/localtime","operator":"in","value":"T{{alarm}}:00/{{schedule.split('/')[0]}}"},
                           {"address":"/sensors/48/state/presence","operator":"eq","value":"true"},
                           {"address":"/sensors/48/state/presence","operator":"dx"},
                           {"address":"/sensors/50/state/status","operator":"lt","value":"1"},
                           {"address":"/sensors/49/state/dark","operator":"eq","value":"true"}] }

#"name":"MotionSensor 48.day-dark-on"
      - service: rest_command.set_hue_rule
        data_template:
          type: rules
          id: 18
          data: >
            {% set schedule = states('sensor.master_bedroom_schedule_rst') %}
            {% set alarm = states('sensor.alarmclock_wd_time') %}
            {"conditions":[{"address":"/config/localtime","operator":"in","value":"T{{alarm}}:00/{{schedule.split('/')[0]}}"},
                           {"address":"/sensors/48/state/presence","operator":"eq","value":"true"},
                           {"address":"/sensors/49/state/dark","operator":"eq","value":"true"},
                           {"address":"/sensors/49/state/dark","operator":"dx"},
                           {"address":"/sensors/50/state/status","operator":"lt","value":"1"}] }

# "name":"MotionSensor 48.night-on"
      - service: rest_command.set_hue_rule
        data_template:
          type: rules
          id: 19
          data: >
            {% set schedule = states('sensor.master_bedroom_schedule_rst') %}
            {% set alarm = states('sensor.alarmclock_wd_time') %}
            {"conditions":[{"address":"/config/localtime","operator":"in","value":"{{schedule.split('/')[0]}}/T{{alarm}}:00"},
                           {"address":"/sensors/48/state/presence","operator":"eq","value":"true"},
                           {"address":"/sensors/48/state/presence","operator":"dx"},
                           {"address":"/sensors/50/state/status","operator":"lt","value":"1"},
                           {"address":"/sensors/49/state/dark","operator":"eq","value":"true"}] }

# "name":"MotionSensor 48.night-dark-on"
      - service: rest_command.set_hue_rule
        data_template:
          type: rules
          id: 20
          data: >
            {% set schedule = states('sensor.master_bedroom_schedule_rst') %}
            {% set alarm = states('sensor.alarmclock_wd_time') %}
            {"conditions":[{"address":"/config/localtime","operator":"in","value":"{{schedule.split('/')[0]}}/T{{alarm}}:00"},
                           {"address":"/sensors/48/state/presence","operator":"eq","value":"true"},
                           {"address":"/sensors/49/state/dark","operator":"eq","value":"true"},
                           {"address":"/sensors/49/state/dark","operator":"dx"},
                           {"address":"/sensors/50/state/status","operator":"lt","value":"1"}]}

and automation:

  - alias: 'Update motion sensor Master bedroom schedule'
    id: 'Update motion sensor Master bedroom schedule'
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: sensor.alarmclock_wd_time
    condition:
      condition: template
      value_template: >
        {{is_state('input_boolean.alarmclock_wd_enabled','on')}}
    action:
      service: script.set_hue_alarm_time_wakeup