Lay-Z-Spa Hot Tub wi-fi pump automation

My current YAML which is working. It’s all based on what Bruce has done but changed where needed to work with the clever spa.

Once i’ve got all the functions working i’ll get a full package of code together.

#####################################
sensor:
  - platform: rest
    name: hottub_status
    scan_interval: 30
    timeout: 20    
    resource: "https://api.gizwits.com/app/devdata/xxxxxxxxxxxx/latest"  
    # xxxxxxxxxxxx is the did from the bindings sensor
    device_class: timestamp
    headers:
      Content-Type: application/json
      X-Gizwits-Application-Id: 805cc6a3f41b48aeae471e2fcb6ebc73
    method: GET
    value_template: "{{ value_json.updated_at | timestamp_custom ('%Y-%m-%dT%H:%M:%S+00:00') }}"
    json_attributes_path: "$.attr"
    json_attributes:
      - Heater
      - 03
      - Undercooling
      - Current_temperature
      - Filter
      - Temperature_setup
      - Superheat
      - Bubble
      - Overtime_filter
      - Timing
      - Check
      - Time_filter

  - platform: template
    sensors:
      hottub_pump_temp:
        availability_template: "{{ states('binary_sensor.hottub_online') }}"
        friendly_name: "HotTub Water Temperature"
        unit_of_measurement: '°C'
        value_template: "{{ state_attr('sensor.hottub_status', 'Current_temperature') | int }}"

      hottub_water_temp:
        availability_template: "{{ states('binary_sensor.hottub_online') }}"
        friendly_name: "HotTub Water Temperature"
        unit_of_measurement: '°C'        
        value_template: "{{ states('input_number.Temperature_setup') | int }}"

      hottub_summary:
        availability_template: "{{ states('binary_sensor.hottub_online') }}"
        friendly_name: "HotTub Status"
        value_template: "{% if is_state('binary_sensor.hottub_online','off') %}offline{% elif is_state('sensor.hottub_error','on') %}error{% elif is_state('input_boolean.hottub_scheduled','on') and is_state('switch.hottub_filter','off') %}scheduled{% elif is_state('switch.hottub_power','off') %}off{% elif state_attr('sensor.hottub_status','heat_temp_reach')==1 %}at temperature{% elif is_state('switch.hottub_heat','on') %}heating{% elif is_state('switch.hottub_filter','off') %}on{% elif is_state('switch.hottub_heat','off') %}filtering{% elif is_state('switch.hottub_bubbles','off') %}heating{% else %}bubbling{% endif %}"

  - platform: history_stats
    name: HotTub Heating This Week
    entity_id: sensor.hottub_summary
    state: "heating"
    type: time
    end: "{{ now().replace(hour=0, minute=0, second=0) }}"
    duration:
      days: 7

rest_command:
  hottub_command: 
    method: POST
    headers:
      content_type: "application/json"
      X-Gizwits-Application-Id: "805cc6a3f41b48aeae471e2fcb6ebc73"
      X-Gizwits-User-token: #####your user token#####
    url: "https://api.gizwits.com/app/control/ZUnVqKmSwvvgGkVtOFxS9v"    
    payload: "{{hottub_command}}"

binary_sensor:
  - platform: template
    sensors:
      hottub_online:
        friendly_name: "HotTub Online"
        value_template: "{{ (now() | as_timestamp - states('sensor.hottub_status') | as_timestamp ) < 2000.0 }}"
        
switch:
  - platform: template
    switches:
      hottub_heat:
        availability_template: "{{ states('binary_sensor.hottub_online') }}"
        unique_id: Heater
        friendly_name: Heater      
        value_template: "{% if state_attr('sensor.hottub_status', 'Heater') == 1 %}on{% else %}off{% endif %}"
        turn_on:
          - service: rest_command.hottub_command
            data_template: 
              hottub_command: >
                {"attrs": {"Heater": 1} }   
          - delay: 00:00:05
          - service: homeassistant.update_entity
            entity_id: sensor.hottub_status
        turn_off:
          - service: rest_command.hottub_command
            data_template: 
              hottub_command: >
                {"attrs": {"Heater": 0} }   
          - delay: 00:00:05
          - service: homeassistant.update_entity
            entity_id: sensor.hottub_status
  
      hottub_filter:
        availability_template: "{{ states('binary_sensor.hottub_online') }}"
        unique_id: hottub_filter
        friendly_name: Filter  
        value_template: "{% if state_attr('sensor.hottub_status', 'filter') == 1 %}on{% else %}off{% endif %}"
        turn_on:
          - service: rest_command.hottub_command
            data_template: 
              hottub_command: >
                {"attrs": {"filter": 1} }   
          - delay: 00:00:05
          - service: homeassistant.update_entity
            entity_id: sensor.hottub_status
        turn_off:
          - service: rest_command.hottub_command
            data_template: 
              hottub_command: >
                {"attrs": {"filter": 0} }   
          - delay: 00:00:05
          - service: homeassistant.update_entity
            entity_id: sensor.hottub_status

      hottub_bubbles:
        availability_template: "{{ states('binary_sensor.hottub_online') }}"
        unique_id: Bubbles
        friendly_name: Bubbles      
        value_template: "{% if state_attr('sensor.hottub_status', 'Bubble') == 1 %}on{% else %}off{% endif %}"
        turn_on:
          - service: rest_command.hottub_command
            data_template: 
              hottub_command: >
                {"attrs": {"Bubble": 1} }   
          - delay: 00:00:05
          - service: homeassistant.update_entity
            entity_id: sensor.hottub_status
        turn_off:
          - service: rest_command.hottub_command
            data_template: 
              hottub_command: >
                {"attrs": {"Bubble": 0} }   
          - delay: 00:00:05
          - service: homeassistant.update_entity
            entity_id: sensor.hottub_status