HAdashboard new widget switch with up to 2 sensors

the third widget i give you to implement is a widget you can use to make to create a switch with up to 2 sensor values. you can add temperature outside and inside on your heatingswitch or the time the switch is automaticly turned on and off (only if you have a sensor with that time in HA) but you could also put the sunset en sunrise times in your switch.
please give me your ideas about combining.

you dont need to change any excisting files.
you need to create a new dir in your widgets dir and give it the name hasensorswitch.
then you make a file called hasensorswitch.coffee with the following code and save it in the new dir.

class Dashing.Hasensorswitch extends Dashing.ClickableWidget
  constructor: ->
    super
    @queryState()

  @accessor 'state',
    get: -> @_state ? 'off'
    set: (key, value) -> @_state = value

  @accessor 'time1',
    get: -> @_time1
    set: (key, value) -> @_time1 = value

  @accessor 'time2',
    get: -> @_time2
    set: (key, value) -> @_time2 = value

  @accessor 'icon',
    get: -> if @['icon'] then @['icon'] else
      if @get('state') == 'on' then @get('iconon') else @get('iconoff')
    set: Batman.Property.defaultAccessor.set

  @accessor 'iconon',
    get: -> @['iconon'] ? 'circle'
    set: Batman.Property.defaultAccessor.set

  @accessor 'iconoff',
    get: -> @['iconoff'] ? 'circle-thin'
    set: Batman.Property.defaultAccessor.set

  @accessor 'icon-style', ->
    if @get('state') == 'on' then 'switch-icon-on' else 'switch-icon-off'    

  toggleState: ->
    newState = if @get('state') == 'on' then 'off' else 'on'
    @set 'state', newState
    return newState

  queryState: ->
    $.get '/homeassistant/switch',
      widgetId: @get('id'),
      (data) =>
        json = JSON.parse data
        @set 'state', json.state
    if typeof @get('timea') isnt 'undefined'
      $.get '/homeassistant/sensor',
        widgetId: @get('timea'),
        (data) =>
          json = JSON.parse data
          @set 'time1', json.value
    if typeof @get('timeb') isnt 'undefined'
      $.get '/homeassistant/sensor',
        widgetId: @get('timeb'),
        (data) =>
          json = JSON.parse data
          @set 'time2', json.value

  postState: ->
    newState = @toggleState()
    $.post '/homeassistant/switch',
      widgetId: @get('id'),
      command: newState,
      (data) =>
        json = JSON.parse data
        if json.error != 0
          @toggleState()

  ready: ->
    if @get('bgcolor')
      $(@node).css("background-color", @get('bgcolor'))
    else
      $(@node).css("background-color", "#444")

  onClick: (event) ->
    @postState()

the next code needs to be saved as hasensortext.html in the new dir

<h1 class="title" data-bind="title"></h1>
<h1 class="title2" data-bind="title2"></h1>

<h2 data-bind-class="icon-style"><i data-bind-class="icon | prepend 'fa fa-'"></i></h2>

<p class="time minus"><i class="title3" data-bind="time1"></i></p>
<p class="time plus"><i class="title3" data-bind="time2"></i></p>

and then you save the next code as hasensorswitch.scss in the new dir.

// ----------------------------------------------------------------------------
// Widget styles
// ----------------------------------------------------------------------------
.widget-hasensorswitch {

  background-color: #444;

  .title {
    color: #fff;
    margin-bottom: 5px;
    margin-top: 5px;
  }

  .title2 {
    color: #fff;
    margin-bottom: 10px;
    margin-top: 0px;
  }
  .title3 {
    color: #fff;
    margin-bottom: 5px;
    margin-top: 10px;
    align: left;
  }

  .switch-icon-off .switch-icon-on {
    font-size: 150%;
  }

  .switch-icon-off {
    color: #000000;
  }

  .switch-icon-on {
  	color: #aaff00;
  }

  .time {
    position: absolute;
    bottom: 3px;
    width: 32px;
    color: #fff;

    &.plus {
      right: 24px;

      i {
        padding-top: 10px;
        padding-left: 10px;
      }
    }

    &.minus {
      left: 8px;

      i {
        padding-top: 10px;
        padding-right: 15px;
      }
    }
  }

}

then you are ready to edit your dashboard like this:

    <li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
      <div data-id="your_switch_id" data-view="Hasensorswitch" data-icon="lightbulb-o" data-timea="your_first_sensor_id" data-timeb="your_second_sensor_id" data-title="any title" data-title2="more text" data-bgcolor="#FFDD55"></div>
    </li>

when i created the widget i only thought about putting time in my switch widget so i could see the time the switch goes on and/or off. thats why i used time in my variable naming.
only after a while i realized that this combination can be used for some more projects.
maybe if it is like enough i will rewrite te naming to a more general nametype.

for those who need the same thing as input_boolean that is also possible.
make a dir with the name hasensorinputboolean, save the files again but with the name changed to hasensorinputboolean and change switch in the code (only at 3 places) for inputboolean.

after that you can use your Hasensorinputboolean in your dashboard.

here you can see how i use it with time off and time on:

4 Likes

@ReneTode ladies and gentleman… the man is on fire!

Thanks for your contributions, Rene!

1 Like

Hello,

I realise this is an old post, but I was wondering if this would work with a timer as the data? I have the timer set up in HA that counts down when an input Boolean is set to on.

It would be great to show the timer as part of the switch in HADashboard.

Thank you for any help

this was for HAdashboard V! which was a complete different program.

at the moment there are no widgets that can have a switch and a timer.
it is possible, but for that you need to program your own custom widget.

how to do that is in the docs here:
https://appdaemon.readthedocs.io/en/latest/WIDGETDEV.html

Thank you for the response.

when you need more help, dont hesitate to ask.
when you do create such a widget please share it, so others can use it too.